package org.session.service.impl;

import org.session.model.RepositoryType;
import org.session.repository.berkeleydb.BerkeleyDBProxyAdapter;
import org.session.repository.couchdb.CouchDBProxyAdapter;
import org.session.repository.drizzle.DrizzleProxyAdapter;
import org.session.repository.hdfs.HDFSProxyAdapter;
import org.session.repository.localfs.LocalFSProxyAdapter;
import org.session.repository.membase.MembaseProxyAdapter;
import org.session.repository.memcached.MemcachedProxyAdapter;
import org.session.repository.mongodb.MongoDBProxyAdapter;
import org.session.repository.redis.RedisProxyAdapter;
import org.session.repository.s3.S3ProxyAdapter;
import org.session.repository.simpledb.SimpleDBProxyAdapter;
import org.session.service.SessionService;
import org.session.util.PropertyLoader;

public final class SessionServiceClient {
    private static final SessionServiceClient INSTANCE = new SessionServiceClient();
    private static final String REPOSITORY_TYPE = PropertyLoader.getProperty(
            "service.properties", "repository.enabled");

    private SessionServiceClient() {
    }

    public static SessionServiceClient getInstance() {
        return INSTANCE;
    }

    public SessionService getSessionService() {
        return getRepository(RepositoryType.valueOf(REPOSITORY_TYPE));
    }

    private SessionService getRepository(final RepositoryType repositoryType) {
        SessionService sessionService = null;
        switch (repositoryType) {
        case BERKELEYDB:
            sessionService = new BerkeleyDBProxyAdapter();
            break;
        case COUCHDB:
            sessionService = new CouchDBProxyAdapter();
            break;
        case DRIZZLE:
            sessionService = new DrizzleProxyAdapter();
            break;
        case HDFS:
            sessionService = new HDFSProxyAdapter();
            break;
        case LOCALFS:
            sessionService = new LocalFSProxyAdapter();
            break;
        case MEMBASE:
            sessionService = new MembaseProxyAdapter();
            break;
        case MEMCACHED:
            sessionService = new MemcachedProxyAdapter();
            break;
        case MONGODB:
            sessionService = new MongoDBProxyAdapter();
            break;
        case REDIS:
            sessionService = new RedisProxyAdapter();
            break;
        case S3:
            sessionService = new S3ProxyAdapter();
            break;
        case SIMPLEDB:
            sessionService = new SimpleDBProxyAdapter();
            break;
        default:
            throw new IllegalArgumentException(
                    "Refer to service.properties for how to set backing session repository");
        }

        return sessionService;
    }

}
